package com.android.ailaoshan.user;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.android.ailaoshan.R;

import java.lang.ref.WeakReference;
import java.util.HashMap;

public class LoginActivity extends AppCompatActivity {

    private Context mContext;

    private TextView loginStatus;

    private DoResponse doResponse = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity);

        // 透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        mContext = LoginActivity.this;

        loginStatus = findViewById(R.id.login_status);
    }

    /**
     * 提交信息
     */
    public void onSubmit(View v){
        EditText uName = findViewById(R.id.login_username);
        EditText uPswd = findViewById(R.id.login_password);

        // 获取内容并去除首尾空格
        String userName = uName.getText().toString().trim();
        String userPswd = uPswd.getText().toString().trim();

        MyHandler myHandler = new MyHandler(loginStatus);
        SendRequest sendRequest = new SendRequest(userName,userPswd,myHandler);
        sendRequest.doRequest();

        loginStatus.setText("正在登录...");

        doResponse = new DoResponse();
    }

    public class MyHandler extends Handler {
        final WeakReference<TextView> loginText;

        MyHandler(TextView textView){
            this.loginText = new WeakReference<>(textView);
        }

        @Override
        public void handleMessage(Message msg){
            HashMap<String,String> responseMap = (HashMap<String, String>) msg.obj;
            switch (msg.what){
                case 300:{
                    // 验证成功
                    if(doResponse != null){
                        switch (responseMap.get("status")){
                            case "OK":{
                                finish();
                                break;
                            }
                            case "ERROR":{
                                doResponse.setStatusText("用户名或密码错误");
                                break;
                            }
                            case "EXCEPTION":{
                                doResponse.setStatusText("服务器处理异常");
                                break;
                            }
                            default:{
                                doResponse.setStatusText("服务器响应错误");
                                break;
                            }
                        }
                    }
                    break;
                }
                case 301:{
                    // 验证失败
                    if(doResponse != null){
                        doResponse.setStatusText("连接服务器失败");
                    }
                    break;
                }
                default:break;
            }
        }
    }

    private class DoResponse{
        /**
         * 设置登录状态提示
         */
        void setStatusText(String txt){
            if(loginStatus != null){
                loginStatus.setText(txt);
            }
        }
    }

    @Override
    protected void onDestroy(){
        super.onDestroy();

        loginStatus = null;
        doResponse = null;
    }
}
